# -*- coding: utf-8 -*-
from spiders.checkpipe import check_spider_pipeline
from twisted.enterprise import adbapi
import MySQLdb
import MySQLdb.cursors


class JinSePipeline(object):
    def __init__(self, dbpool):
        self.dbpool = dbpool

    @classmethod
    def from_settings(cls, settings):
        dbparams = dict(
            host=settings['MYSQL_HOST'],  # 读取settings中的配置
            db=settings['MYSQL_DBNAME'],
            user=settings['MYSQL_USER'],
            passwd=settings['MYSQL_PASSWD'],
            charset='utf8mb4',  # 编码要加上，否则可能出现中文乱码问题
            cursorclass=MySQLdb.cursors.DictCursor,
            use_unicode=False,
        )
        dbpool = adbapi.ConnectionPool('MySQLdb', **dbparams)  # **表示将字典扩展为关键字参数,相当于host=xxx,db=yyy....
        return cls(dbpool)  # 相当于dbpool付给了这个类，self中可以得到

    # pipeline默认调用
    def process_item(self, item, spider):
        query = self.dbpool.runInteraction(self._conditional_insert, item)  # 调用插入的方法
        query.addErrback(self._handle_error, item, spider)  # 调用异常处理方法
        return item

    # 写入数据库中
    def _conditional_insert(self, tx, item):
        # sql = "INSERT INTO spiders_article(title,create_time,source,content,top_notice,weight) VALUES (%s,%s,%s,%s,%s,%s)"
        # params = (item["title"], item["create_time"], item["source"], item["content"], item["is_top"],
        #           item["weight"])
        print(item)
        sql = "INSERT INTO spiders_article(title,content) VALUES (%s,%s)"
        params = (item["title"], item["content"])
        tx.execute(sql, params)

    # 错误处理方法
    def _handle_error(self, failue, item, spider):
        print('--------------JinSe database operation exception!!-----------------')
        print(failue)
